class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        int n = arr.size();
        if(n == 0) return 0;

        unordered_map<int, int> hash;
        hash[arr[0]] = 1;

        int _max = 1;
        for(int i = 1; i < n; i++)
        {
            hash[arr[i]] = hash[arr[i] - difference] + 1;
            _max = max(hash[arr[i]], _max);
        }

        return _max;
    }
};